@using OrchardCore.ContentManagement.Display
@using OrchardCore.Mvc.Utilities

@inject IContentItemDisplayManager ContentItemDisplayManager

@{
    if (Model.Content is not IShape content ||
        Model.ContentItem is not ContentItem contentItem ||
        contentItem.As<AddMetadataWidget>() is not { } widget ||
        content.GetBagPartViewModel() is not { } bagPartViewModel)
    {
        return;
    }

    var widgetClass = "widget-" + nameof(AddMetadataWidget).HtmlClassify();
    var inheritedClasses = (Model.Classes as IEnumerable<string> ?? [])
        .Where(item => item is not ("widget" or "widget-add-metadata-widget"));
    widget.Classes.Text = $"widget {widgetClass} {string.Join(' ', inheritedClasses)} {widget.Classes.Text}";

    bagPartViewModel.Settings = new()
    {
        ContainedStereotypes = bagPartViewModel.Settings.ContainedStereotypes,
        ContainedContentTypes = bagPartViewModel.Settings.ContainedContentTypes,
        DisplayType = widget.DisplayType.Text?.Trim() ?? string.Empty,
    };

    var bagShapes = await ContentItemDisplayManager.DisplayBagPartContentItemsAsync(bagPartViewModel);
}

@foreach (var shape in bagShapes)
{
    widget.UpdateShape(shape, bagPartViewModel);
    @await DisplayAsync(shape)
}


